﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Entity;
using Job.Logic.Common;
using System.Data;
using Job.Web.Framework;
using Job.Data.Interfaces;

namespace Job.Logic.Personal
{
    public class ApplyJobLogic
    {
        public void SendResume(JobSendResume info)
        {
            IJobSendResumeDao dao = DataAccess.sendResumeDao;
            int count = dao.GetExists(info);
            if (count <= 0)
                dao.Insert(info);
            else
                dao.Update(info);
        }

        public void DeleteSendByPerson(JobSendResume info)
        {
            DataAccess.sendResumeDao.DeleteByPerson(info);
        }

        public void DeleteSendByAdmin(JobSendResume info)
        {
            DataAccess.sendResumeDao.Delete(info);
        }

        public int CountSendByPerson(int personAccountId)
        {
            string sql = "Select * From job_send_resume Where person_account_id="
                + personAccountId.ToString();

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, 10, 1, 1,
                "send_time", SortDirection.Descending, null);
            return (Int32)ds.Tables[0].Rows[0][0];
        }


        public DataTable GetPageSendByPerson(int personAccountId, int pageSize, int pageIdx)
        {
            string sql = "Select A.send_id,B.resume_name,A.send_time,C.name,C.job_no,A.person_info_id "
                + "From job_send_resume A,job_person_info B,job_hunter C "
                + "Where A.person_info_id = B.person_info_id And A.job_id=C.job_id "
                + "And A.person_account_id=" + personAccountId;

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIdx, 0,
                "send_time", SortDirection.Descending, null);

            return ds.Tables[0];
        }


        public void AddFavorite(JobPersonFavorites favorite)
        {
            IJobPersonFavoritesDao dao = DataAccess.personFavoritesDao;
            int count = dao.GetExistsSum(favorite.PersonAccountId, favorite.JobId);
            if (count <= 0)
                dao.Insert(favorite);
            else
                dao.UpdateFavorite(favorite.PersonAccountId, favorite.JobId);
        }
        public void DeleteFavoriteByPerson(JobPersonFavorites favorite)
        {
            DataAccess.personFavoritesDao.DeleteByPerson(favorite);
        }

        public int CountFavoritesByPerson(int personAccountId)
        {
            string sql = "select * from job_person_favorites Where person_account_id="
                + personAccountId.ToString();

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, 10, 1, 1,
                "favorite_time", SortDirection.Descending, null);
            return (Int32)ds.Tables[0].Rows[0][0];
        }

        public DataTable GetPageFavoritesByPerson(int personAccountId, int pageSize, int pageIdx)
        {
            string sql = "Select A.favorites_id,A.favorite_time,B.name,A.job_id,B.job_no "
                + "From job_person_favorites A,job_hunter B "
                + "Where A.job_id = B.job_id And A.person_account_id="
                + personAccountId.ToString();


            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIdx, 0,
                "favorite_time", SortDirection.Descending, null);

            return ds.Tables[0];
        }


        public int CountSendByJob(JobSendResume send)
        {
            string sql = "Select * From job_send_resume A Where " 
                + GetWhereByJob(send) + " And A.job_id=" + send.JobId.ToString();

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, 10, 1, 1,
                "send_time", SortDirection.Descending, null);
            return (Int32)ds.Tables[0].Rows[0][0];
        }

        public DataTable GetPageSendByJob(JobSendResume send, int pageSize, int pageIdx)
        {
            string sql = "Select A.send_id,B.user_name,B.resume_name,A.send_time,A.person_info_id,A.send_status "
                + "From job_send_resume A,job_person_info B Where A.person_info_id = B.person_info_id And " 
                + GetWhereByJob(send) + "And A.job_id=" + send.JobId.ToString();

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIdx, 0,
                "send_time", SortDirection.Descending, null);

            return ds.Tables[0];
        }

        private String GetWhereByJob(JobSendResume send)
        {
            StringBuilder buf = new StringBuilder();
            buf.Append(" 1= 1");
            if (send.SendFrom != null && send.SendFrom.Year > 2008)
            {
                buf.Append(" And A.send_time >= '");
                buf.Append(send.SendFrom.ToString("yyyy-MM-dd"));
                buf.Append("' ");
            }
            if (send.SendTo != null && send.SendTo.Year > 2008)
            {
                buf.Append(" And A.send_time <= '");
                buf.Append(send.SendTo.ToString("yyyy-MM-dd"));
                buf.Append(" 23:59:59' ");
            }
            if (send.SendStatus != null && send.SendStatus.Length > 0)
            {
                buf.Append(" And A.send_status='");
                buf.Append(send.SendStatus);
                buf.Append("' ");
            }
            return buf.ToString();
        }


        public void UpdateSendStatus(int SendId, char status)
        {
            JobSendResume send = new JobSendResume();
            send.SendId = SendId;
            send.SendStatus = new String(status, 1);

            DataAccess.sendResumeDao.UpdateStatus(send);
        }
    }
}
